package pl.edu.romanow.query

import xml.NodeSeq
import com.tinkerpop.gremlin.scala.GremlinScalaPipeline
import net.liftweb.util.Helpers._
import net.liftweb.http.{SHtml, Templates}
import net.liftweb.common.{Empty, Full, Box}
import net.liftweb.http.js.JsCmds
import com.tinkerpop.blueprints.pgm.{Vertex, Edge}
import collection.mutable.Set

/**
 * Created by IntelliJ IDEA.
 * User: jar
 * Date: 29.04.12
 * Time: 11:22
 * To change this template use File | Settings | File Templates.
 */

case class ConnectionItem(var connection: Box[String], override var take: Boolean) extends QueryItem {

  def this(conn: String) = this(Full(conn), true)

  val path = "items" :: "connection" :: Nil


  override def bind(selected: Boolean = true): NodeSeq =
    (
      "#connection_label" #> SHtml.ajaxText(connection.openOr("all"), sel => {
        connection = (if (sel == "all") Empty else Full(sel: String))
        JsCmds.Noop
      }) &
        "#connection_take" #> SHtml.ajaxCheckbox(take, sel => {
          take = sel
          JsCmds.Noop
        })

      )(Templates(path).open_!)


  def edges(pipe: GremlinScalaPipeline[Edge, Edge], toDisplay: Set[Edge]) = {
    val base = connection match {
      case Full(cnn) => pipe.bothV().bothE(cnn)
      case _ => pipe.bothV().bothE()
    }
    if (take) {
      base.sideEffect((e: Edge) => toDisplay += e)
    }
    else
      base

  }
}
